<?php
namespace Admin\Model;
use Think\Model;
class LinksModel extends Model{
    //查询方法
    public function links_sel(){
        // 得到总行数
    $totalRow = $this->count();
    // 每页显示条数
    $num = 10;
    // 实例化分页类
    $page = new \Think\Page($totalRow , $num);
    // 执行分页查询
    $list = $this->limit( $page->firstRow . ',' . $page->listRows )->select();
    //设置数据状态
    $status = ['1'=>'新添加','2'=>'显示','3'=>'不显示'];
    foreach($list as $key => &$val){
        $val['status'] = $status[ $val['status'] ];
        $val['addtime'] = date('Y-m-d H:i:s',$val['addtime']);
    }
    return [
        'list'=> $list,
        'show'=> $page->show(),
    ];
    }
    //执行添加方法
    public function links_add(){
        $links = I('post.');
        //判断获取过来的值是否有空
        if(empty($links['linkname'])||empty($links['linkman'])||empty($links['linkaddress'])||empty($links['email'])||empty($links['linkname'])){
            return false;
        }
        //判断邮箱格式
        $pattern = "/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/";
        if(!preg_match( $pattern, $links['email'])){
            return false;
        }
        //判断链接格式，必须由英文，.//组成
        $preg = "/^((http|ftp|https):\/\/)?[\w-_.]+(\/[\w-_]+)*\/?$/";
        if(!preg_match( $preg, $links['linkaddress'])){
            return false;
        }
        //获取当天时间戳
        // 写进数组
        $links['addtime'] = time();
         //取出 地址
        $address = $links['linkaddress'];
        //清除最左边的http:://
        $address = ltrim($address,'http://');
        //给地址添加上http://
        $links['linkaddress'] = 'http://'.$address;
        $res = $this->add($links);
        return $res;   
    }
    //执行删除方法
    public function links_del(){
        //获取待删除数据id
        $id = I('get.id');
        //设置删除条件
        $map['id'] = ['eq',$id];
        //执行删除并接收返回值
        $res = $this->where($map)->delete();
        //判断是否删除成功
        if($res){
            return '删除成功';
        }else{
            return '删除失败';
        }

    }
    //查询一条数据方法
    public function links_find(){
        $id = I('get.id');
        //设置查询条件
        $map['id'] = ['eq',$id];
        //发送执行
        $list = $this->where($map)->find();
        //设置状态
        // $status = ['1'=>'新添加','2'=>'显示','3'=>'不显示'];
        // foreach($list as $key=>&$val){
        //     $val['status'] = $status[$val['status']];
        // }
        return $list;
    }
    //执行修改方法
    public function links_update(){
        $id = I('post.id');
        $data['linkname'] = I('post.linkname');
        $data['linkman'] = I('post.linkman');
        $data['email'] = I('post.email');
        $data['linkaddress'] = I('post.linkaddress');
        $map['id'] = ['eq',$id];
        //判断邮箱格式
        $pattern = "/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/";
        if(!preg_match( $pattern, $data['email'])){
            return false;
        }
        //判断链接格式，必须由英文，.//组成
        $preg = "/^((http|ftp|https):\/\/)?[\w-_.]+(\/[\w-_]+)*\/?$/";
        if(!preg_match( $preg, $data['linkaddress'])){
            return false;
        }
        //发送执行
        $res = $this->where($map)->save($data);
        return $res;
    }
}